<?php
/**
 * Created by PhpStorm.
 * User: 65736
 * Date: 2019/3/8 0008
 * Time: 15:17
 */

namespace app\common\widget;

use Exception;

/**
 * Class WidgetIcons
 * @package app\common\widget
 * @method WidgetIcons setColorPrimary()
 * @method WidgetIcons setColorDefault()
 * @method WidgetIcons setColorSuccess()
 * @method WidgetIcons setColorInfo()
 * @method WidgetIcons setColorDanger()
 *
 * @method WidgetIcons setfaCheckIcon()
 * @method WidgetIcons setfaCloseIcon()
 * @method WidgetIcons setfaLongArrowUpIcon()
 * @method WidgetIcons setfaLongArrowDownIcon()
 * @method WidgetIcons setfaShareAltIcon()
 * @method WidgetIcons setfaEditIcon()
 * @method WidgetIcons setfaEyeIcon()
 * @method WidgetIcons setfaLinkIcon()
 *
 */
class WidgetIcons extends WidgetCommon
{

    /**
     * ICON颜色样式
     * @var string
     */
    private $colorStyle = WidgetConst::textPrimary;

    /**
     * ICON图标样式
     * @var string
     */
    private $iconStyle = WidgetConst::faEdit;

    /**
     * 构造方法, 渲染挂件
     * @return mixed
     * @throws \think\Exception
     */
    function render()
    {
        // 分配样式
        $this->setClass( $this->iconStyle );
        // 分配颜色
        $this->setClass( $this->colorStyle );

        return $this->fetch();
    }

    /**
     * @throws \think\Exception
     */
    protected function initialize()
    {
        parent::initialize();
        // 设置默认class
        $this->setClass(['fa', 'fa-fw']);
    }

    /**
     * 设置一个ICON
     * @param $style
     * @return $this
     */
    private function setIcon( $style ) {
        $this->iconStyle = $style;
        return $this;
    }

    /**
     * 设置ICON颜色
     * @param $color
     * @return $this
     */
    private function setColor( $color ) {
        $this->colorStyle = $color;
        return $this;
    }

    /**
     * 未定义方法回调
     * @param $name
     * @param $arguments
     * @return WidgetIcons
     * @throws Exception
     */
    public function __call($name, $arguments)
    {
        // 匹配color方法
        if ( strpos( $name,"setColor" ) !== false ) {
            $color = str_replace( 'setColor', '', $name );
            $const = "text".$color;
            $argument = WidgetConst::getConstant($const);
            return $this->setColor( $argument );
        }

        // 匹配样式
        if ( strpos( $name, "setfa" ) !== false ) {
            $const = str_replace(['Icon','set'], '', $name);
            $argument = WidgetConst::getConstant($const);
            return $this->setIcon($argument);
        }

        return $this;
    }


}